package com.whp.openapi.service.impl;

import com.whp.openapi.common.ResponseResult;
import com.whp.openapi.common.ResultTable;
import com.whp.openapi.dao.MenuMapper;
import com.whp.openapi.entity.Menu;
import com.whp.openapi.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author whp
 * @date 2021/11/24
 */
@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;
    
    //权限树
    @Override
    public List<Menu> getAllMenuTree() {
        //获取所有权限
        List<Menu> all = menuMapper.getAllMenu();
        //获取父级权限
        List<Menu> fu = new ArrayList<>();
        for (Menu menu : all) {
            if (menu.getParentId() == null) {
                fu.add(menu);
            }
        }
        //获取子级权限
        for (Menu menu : fu) {
            List<Menu> zis = new ArrayList<>();
            //遍历所有权限
            for (Menu zi : all) {
                //找到父节点的子级权限
                if (zi.getParentId() != null && zi.getParentId().equals(menu.getId())) {
                    zis.add(zi);
                }
            }
            //将子级权限赋值给父节点
            menu.setChildren(zis);
        }
        return fu;
    }

    //-----------------------stream流---------------------------
    @Override
    public List<Menu> getMenuByUid(Integer id) {
        //获取所有菜单
        List<Menu> all = menuMapper.getMenuByUid(id);
        //获取父级菜单
        List<Menu> collect = all.stream()
                .filter(x -> x.getParentId() == null)
                .map(x -> findChildren(x, all))
                .collect(Collectors.toList());
        return collect;
    }
    //查找子级菜单
    public Menu findChildren(Menu menu, List<Menu> all) {
        List<Menu> collect = all.stream()
                .filter(x -> x.getParentId() != null && menu.getId().equals(x.getParentId()) && x.getType() != 2)
                .map(x -> findChildren(x, all))
                .collect(Collectors.toList());
        menu.setChildren(collect);
        return menu;
    }

    //-----------------------循环方式（未写递归）---------------------------
/*    
    @Override
    public List<Menu> getMenuByUid(Integer id) {
        //获取所有菜单
        List<Menu> all = menuMapper.getMenuByUid(id);
        
        //获取父级菜单
        List<Menu> fu = new ArrayList<>();
        for (Menu menu : all) {
            if (menu.getParentId() == null) {
                fu.add(menu);
            }
        }
        //获取子级菜单
        for (Menu menu : fu) {
            List<Menu> zis = new ArrayList<>();
            for (Menu zi : all) {
                if (zi.getParentId() != null && zi.getParentId().equals(menu.getId()) && zi.getType() != 2) {
                    zis.add(zi);
                }
            }
            //将子级菜单赋值给父级菜单
            menu.setChildren(zis);
        }
        return fu;
    }*/

    @Override
    public ResultTable getList(Integer page, Integer limit) {
        List<Menu> menuList = menuMapper.getList();
        return new ResultTable(0, " ", menuList.size(), menuList);
    }

    @Override
    public Menu getMenuById(Integer id) {
        return menuMapper.selectByPrimaryKey(id);
    }

    @Override
    public ResponseResult delMenus(Integer[] ids) {
        int i = menuMapper.delMenus(ids);
        if (i > 0) {
            return ResponseResult.getSuccess();
        }
        return ResponseResult.getFail("删除失败");
    }

    @Override
    public ResponseResult addMenu(Menu menu) {
        if (menu != null) {
            int i = menuMapper.insertSelective(menu);
            if (i > 0) {
                return ResponseResult.getSuccess();
            }
        }
        return ResponseResult.getFail("添加失败");
    }

    @Override
    public ResponseResult updateMenu(Menu menu) {
        if (menu != null) {
            int i = menuMapper.insertSelective(menu);
            if (i > 0) {
                return ResponseResult.getSuccess();
            }
        }
        return ResponseResult.getFail("编辑失败");
    }
}
